package com.token.zhuishushenqi.community.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.token.zhuishushenqi.R;
import com.token.zhuishushenqi.community.adapter.HotListViewAdapter;
import com.token.zhuishushenqi.community.entity.DynamicHotJson;
import com.token.zhuishushenqi.community.util.CommunityHttpUtils;
import com.token.zhuishushenqi.other.asyn.ZhuishuTask;
import com.token.zhuishushenqi.other.util.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 热门的fragment
 * Created by pengjianhuan on 2016/11/8 0008.
 */

public class HotFragment extends Fragment {

    private ZhuishuTask zhuishuTaskJson;

    private ZhuishuTask zhuishuTaskImage;

    private HotListViewAdapter hotListViewAdapter;

    private ListView listView;

    List<DynamicHotJson> hotJsonList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        hotJsonList = new ArrayList<>();

        return inflater.inflate(R.layout.fragment_hot,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listView = (ListView) view.findViewById(R.id.hot_lv);
        downloadJson();

        hotListViewAdapter = new HotListViewAdapter(getActivity(),hotJsonList);

        listView.setAdapter(hotListViewAdapter);

    }

    public void downloadJson(){
        zhuishuTaskJson = new ZhuishuTask(new ZhuishuTask.IRequest() {
            @Override
            public Object doRequest() {

                return HttpUtils.doGet(CommunityHttpUtils.HOT_URL);
            }

            @Override
            public Object parseResult(Object obj) {
                String json = obj.toString();
                try {
                    JSONObject root = new JSONObject(json);
                    boolean ok = root.getBoolean("ok");
                    if (ok){
                        //JSONObject tweets = root.getJSONObject("tweets");
                        DynamicHotJson dynamicHotJson = DynamicHotJson.objectFromData(root.toString(), "tweets");
                        String avatar = dynamicHotJson.getUser().getAvatar();//头像地址，需要拼接
                        TextView tvNickName = new TextView(getContext());
                        tvNickName.setText(dynamicHotJson.getUser().getNickname());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }
        }, new ZhuishuTask.IRequestCallback() {
            @Override
            public void onSuccess(Object object) {


                Toast.makeText(getActivity(), "请求成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError() {
                Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
        zhuishuTaskJson.execute();

    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (zhuishuTaskJson!=null){
            zhuishuTaskJson.cancel(true);
            zhuishuTaskJson = null;
        }
        hotListViewAdapter.cancel();
    }
}
